package eu.darken.octi.sync.core

import android.content.Context
import com.gitee.wsl.exception.logTag
import com.gitee.wsl.android.ext.ApplicationInit.application
import com.gitee.wsl.android.ext.ApplicationInit.globalStoreServer
import com.gitee.wsl.data.store.StoreServer
import com.gitee.wsl.data.store.saveAble
import timber.log.Timber

class SyncSettings constructor(
    private val context: Context = application,
     val dataStoreServer: StoreServer = globalStoreServer
)  {

    //private val Context.dataStore by preferencesDataStore(name = "sync_settings")

    /*override val dataStore: DataStore<Preferences>
        get() = context.dataStore*/

    val deviceLabel:String by saveAble(dataStoreServer,"sync.device.self.label", "")

    val backgroundSyncEnabled:Boolean  by saveAble(dataStoreServer,"sync.background.enabled", true)

    val backgroundSyncInterval:Int by saveAble(dataStoreServer,"sync.background.interval.minutes", 60)

    val backgroundSyncOnMobile:Boolean  by saveAble(dataStoreServer,"sync.background.mobile.enabled", true)

   /* override val mapper: PreferenceStoreMapper = PreferenceStoreMapper(
        deviceLabel,
        backgroundSyncEnabled,
        backgroundSyncInterval,
        backgroundSyncOnMobile,
    )*/

    val deviceId:DeviceId by saveAble(dataStoreServer,"sync.identifier.device", true){
        DeviceId().also { Timber.d( "Our DeviceId is $it") }
    }

   /* val deviceId:DeviceId by lazy {
        val key = stringPreferencesKey("sync.identifier.device")
        val rawId = runBlocking {
            dataStore.edit { prefs ->
                prefs[key] ?: kotlin.run {
                    UUID.randomUUID().toString().also {
                        prefs[key] = it
                    }
                }
            }[key]!!
        }
        DeviceId(rawId).also { Timber.d( "Our DeviceId is $it") }
    }*/

    companion object {
        internal val TAG = logTag("Sync", "Settings")
    }
}